import {
  Get,
  Post,
  Patch,
  Param,
  Delete,
  Body,
  DefaultValuePipe,
  ParseIntPipe,
} from '@nestjs/common';
import { MenuService } from './menu.service';
import { CreateMenuDto } from './dto/create-menu.dto';
import { UpdateMenuDto } from './dto/update-menu.dto';
import { WitAdmin } from '../auth/wit.admin';
import { Menu } from './entities/menu.entity';
import { GetUser } from '../auth/get-user.decorator';
import { User } from '../user/entities/user.entity';
import { Pagination } from '../utils/paginate';
import { ThemeFilterDto } from '../theme/dto/themeFilterDto';
import { ApiOperation } from '@nestjs/swagger';
import { BindRoleDto } from './dto/bind-role.dto';
import { BindMenuDto } from './dto/bind-menu.dto';

@WitAdmin('menu')
export class MenuController {
  constructor(private readonly menuService: MenuService) {}

  @Post('/createMenu')
  @ApiOperation({
    summary: '添加菜单',
  })
  create(
    @Body() createMenuDto: CreateMenuDto,
    @GetUser() user: User,
  ): Promise<string> {
    return this.menuService.create(createMenuDto, user);
  }

  @Post('/getMenuList')
  @ApiOperation({
    summary: '获取菜单列表',
  })
  findAll(
    @Body() data: ThemeFilterDto,
    @Param('pageNum', new DefaultValuePipe(0), ParseIntPipe) pageNum?: number,
    @Param('pageSize', new DefaultValuePipe(0), ParseIntPipe) pageSize?: number,
  ): Promise<Pagination<Menu>> {
    const page = {
      page: pageNum,
      limit: pageSize > 100 ? 100 : pageSize,
    };
    return this.menuService.findAll(data, page);
  }
  @Get('/getMenuTree')
  @ApiOperation({
    summary: '获取菜单树',
  })
  findAllTree(): Promise<Menu[]> {
    return this.menuService.findAllTree();
  }
  @Get('/getMenu/:id')
  @ApiOperation({
    summary: '获取菜单',
  })
  findOne(@Param('id') id: string): Promise<Menu> {
    return this.menuService.findOne(id);
  }

  @Post('/updateMenu')
  @ApiOperation({
    summary: '修改菜单',
  })
  update(@Body() data: UpdateMenuDto, @GetUser() user: User): Promise<string> {
    return this.menuService.update(data, user);
  }

  @Delete('/deleteMenu/:id')
  @ApiOperation({
    summary: '删除菜单',
  })
  remove(@Param('id') id: string): Promise<string> {
    return this.menuService.remove(id);
  }

  @Post('/bindRole')
  @ApiOperation({
    summary: '菜单绑定到角色',
  })
  bindRole(@Body() data: BindRoleDto): Promise<string> {
    return this.menuService.bindRoles(data);
  }
  @Post('/bindMenu')
  @ApiOperation({
    summary: '菜单绑定父子级绑定',
  })
  bindMenu(@Body() data: BindMenuDto, @GetUser() user: User): Promise<string> {
    return this.menuService.bindTree(data.parentId, null, data.menuId, user);
  }
}
